﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Oracle.ManagedDataAccess.Client;

namespace YJ.Data.ORACLE
{
    public class SMSLog : YJ.Data.Interface.ISMSLog
    {
        private DBHelper dbHelper = new DBHelper();
        /// <summary>
        /// 构造函数
        /// </summary>
        public SMSLog()
        {
        }
        /// <summary>
        /// 添加记录
        /// </summary>
        /// <param name="model">YJ.Data.Model.SMSLog实体类</param>
        /// <returns>操作所影响的行数</returns>
        public int Add(YJ.Data.Model.SMSLog model)
        {
            string sql = @"INSERT INTO SMSLog
				(ID,MobileNumber,Contents,SendUserID,SendUserName,SendTime,Status,Note) 
				VALUES(:ID,:MobileNumber,:Contents,:SendUserID,:SendUserName,:SendTime,:Status,:Note)";
            OracleParameter[] parameters = new OracleParameter[]{
				new OracleParameter(":ID", OracleDbType.Varchar2){ Value = model.ID },
				new OracleParameter(":MobileNumber", OracleDbType.Varchar2){ Value = model.MobileNumber },
				new OracleParameter(":Contents", OracleDbType.NVarchar2){ Value = model.Contents },
				model.SendUserID == null ? new OracleParameter(":SendUserID", OracleDbType.Varchar2) { Value = DBNull.Value } : new OracleParameter(":SendUserID", OracleDbType.Varchar2) { Value = model.SendUserID },
				model.SendUserName == null ? new OracleParameter(":SendUserName", OracleDbType.NVarchar2, 1000) { Value = DBNull.Value } : new OracleParameter(":SendUserName", OracleDbType.NVarchar2) { Value = model.SendUserName },
				new OracleParameter(":SendTime", OracleDbType.Date){ Value = model.SendTime },
				new OracleParameter(":Status", OracleDbType.Int32){ Value = model.Status },
                new OracleParameter(":Note", OracleDbType.Varchar2){ Value = model.Note },
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 更新记录
        /// </summary>
        /// <param name="model">YJ.Data.Model.SMSLog实体类</param>
        public int Update(YJ.Data.Model.SMSLog model)
        {
            string sql = @"UPDATE SMSLog SET 
				MobileNumber=:MobileNumber,Contents=:Contents,SendUserID=:SendUserID,SendUserName=:SendUserName,SendTime=:SendTime,Status=:Status,Note=:Note
				WHERE ID=:ID";
            OracleParameter[] parameters = new OracleParameter[]{
				new OracleParameter(":MobileNumber", OracleDbType.Varchar2){ Value = model.MobileNumber },
				new OracleParameter(":Contents", OracleDbType.NVarchar2){ Value = model.Contents },
				model.SendUserID == null ? new OracleParameter(":SendUserID", OracleDbType.Varchar2) { Value = DBNull.Value } : new OracleParameter(":SendUserID", OracleDbType.Varchar2) { Value = model.SendUserID },
				model.SendUserName == null ? new OracleParameter(":SendUserName", OracleDbType.NVarchar2) { Value = DBNull.Value } : new OracleParameter(":SendUserName", OracleDbType.NVarchar2) { Value = model.SendUserName },
				new OracleParameter(":SendTime", OracleDbType.Date){ Value = model.SendTime },
				new OracleParameter(":Status", OracleDbType.Int32){ Value = model.Status },
                new OracleParameter(":Note", OracleDbType.Varchar2){ Value = model.Note },
				new OracleParameter(":ID", OracleDbType.Varchar2){ Value = model.ID }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 删除记录
        /// </summary>
        public int Delete(Guid id)
        {
            string sql = "DELETE FROM SMSLog WHERE ID=:ID";
            OracleParameter[] parameters = new OracleParameter[]{
				new OracleParameter(":ID", OracleDbType.Varchar2){ Value = id }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 将DataRedar转换为List
        /// </summary>
        private List<YJ.Data.Model.SMSLog> DataReaderToList(OracleDataReader dataReader)
        {
            List<YJ.Data.Model.SMSLog> List = new List<YJ.Data.Model.SMSLog>();
            YJ.Data.Model.SMSLog model = null;
            while (dataReader.Read())
            {
                model = new YJ.Data.Model.SMSLog();
                model.ID = dataReader.GetGuid(0);
                model.MobileNumber = dataReader.GetString(1);
                model.Contents = dataReader.GetString(2);
                if (!dataReader.IsDBNull(3))
                    model.SendUserID = dataReader.GetGuid(3);
                if (!dataReader.IsDBNull(4))
                    model.SendUserName = dataReader.GetString(4);
                model.SendTime = dataReader.GetDateTime(5);
                model.Status = dataReader.GetInt32(6);
                model.Note = dataReader.GetString(7);
                List.Add(model);
            }
            return List;
        }
        /// <summary>
        /// 查询所有记录
        /// </summary>
        public List<YJ.Data.Model.SMSLog> GetAll()
        {
            string sql = "SELECT * FROM SMSLog";
            OracleDataReader dataReader = dbHelper.GetDataReader(sql);
            List<YJ.Data.Model.SMSLog> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List;
        }
        /// <summary>
        /// 查询记录数
        /// </summary>
        public long GetCount()
        {
            string sql = "SELECT COUNT(*) FROM SMSLog";
            long count;
            return long.TryParse(dbHelper.GetFieldValue(sql), out count) ? count : 0;
        }
        /// <summary>
        /// 根据主键查询一条记录
        /// </summary>
        public YJ.Data.Model.SMSLog Get(Guid id)
        {
            string sql = "SELECT * FROM SMSLog WHERE ID=:ID";
            OracleParameter[] parameters = new OracleParameter[]{
				new OracleParameter(":ID", OracleDbType.Varchar2){ Value = id }
			};
            OracleDataReader dataReader = dbHelper.GetDataReader(sql, parameters);
            List<YJ.Data.Model.SMSLog> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List.Count > 0 ? List[0] : null;
        }
    }
}